package com.fortune.controller;


import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.enums.Shelf;
import com.fortune.dto.excution.ProductExcution;
import com.fortune.dto.in.dealaerProduce.*;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.ProductEnum;
import com.fortune.service.DSpecificationService;
import com.fortune.service.DUnitService;
import com.fortune.service.MProductInfoService;
import com.fortune.vo.dProduct.*;
import com.fortune.vo.productMgt.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value = "dProduct", description = "经销商商品管理", tags = {"经销商商品管理相关"})
@RestController
@RequestMapping("dProduct")
public class DelaerProductController {
	@Autowired
	private MProductInfoService mProductInfoService;
	@Autowired
	private DSpecificationService dSpecificationService;
	@Autowired
	private DUnitService dUnitService;


	@ApiOperation(value = "经销商添加商品", nickname = "dProductAdd", notes = "" ,tags = {"@石匡"})
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DProductAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		int num = mProductInfoService.queryDProductByName(did,addDto.getName());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.queryDProductByNumber(did,addDto.getNumber());
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_EXIST);
		}
		num = mProductInfoService.addDProduce(did,addDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "经销商编辑商品", nickname = "dProductEdit", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DProductEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		int num = mProductInfoService.queryProductByDProduceId(did,editDto.getId());
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		num = mProductInfoService.queryEditDProductByName(did,editDto);
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NAME_EXIST);
		}
		num = mProductInfoService.queryEditDProductByNumber(did,editDto);
		if (num > 0){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NUMBER_EXIST);
		}
		num = mProductInfoService.editProductByDProduceIdAndProduceId(did,editDto);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "经销商删除商品", nickname = "dProductDelete", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParam(name="produceId", value="商品id",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品id不能为空") String produceId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		int num = mProductInfoService.queryProductByDProduceId(did,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.PRODUCE_NO_EXIST);
		}
//		ProductStockDetailInfo detailInfo = mProductInfoService.queryIsStockExist(did,produceId);
//		if (detailInfo != null){
//			int remainNum = detailInfo.getRemainNum();
//			if (remainNum > 0){
//				return new ProductExcution<Void>(null,ProductEnum.PRODUCE_STOCK_EXIST);
//			}
//		}
		num = mProductInfoService.deleteDProduce(did,produceId);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}


	@ApiOperation(value = "经销商上下架商品", nickname = "dProductUpAndDown", notes = "" ,tags = {"@石匡"})
	@PutMapping(value = "mProductUpAndDown",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> upAndDown(HttpServletRequest request,@Validated @RequestBody DProductUpAndDown upAndDown) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		Product product = mProductInfoService.queryDProduceInfo(did,upAndDown.getId());
		if (product==null){
			return new ProductExcution(null,ProductEnum.PRODUCE_NO_EXIST);
		}
		Shelf shelf = product.getShelf();
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("YES")){
			return new ProductExcution(null,ProductEnum.UP);
		}
		if (shelf.toString().equals(upAndDown.getType().toString()) && upAndDown.getType().toString().equals("NO")){
			return new ProductExcution(null,ProductEnum.DOWN);
		}
		int num = mProductInfoService.upAndDownDProduce(did,upAndDown);
		if (num < 1){
			return new ProductExcution<Void>(null,ProductEnum.FAILURE);
		}
		return new ProductExcution<Void>(null,ProductEnum.SUCCESS);
	}


	@ApiOperation(value = "商品列表", nickname = "dproductList", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="ppcid", value="商品分类ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="string")
	})
	@GetMapping(value = "list",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<Product>> list(HttpServletRequest request,@ApiIgnore @Validated DProductListDto productListDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		productListDto.setPage((productListDto.getPage()-1)*10);

		int num = mProductInfoService.queryProductCategory(productListDto.getPpcid());
		List<Product> productList = new ArrayList<>();
		int count = 0;
		if (num > 0){
			productList = mProductInfoService.queryDProductInfoFuList(did,productListDto);
			count = mProductInfoService.queryDProductInfoListFuCount(did,productListDto);
		} else {
			productList = mProductInfoService.queryDProductInfoList(did,productListDto);
			count = mProductInfoService.queryDProductInfoListCount(did,productListDto);
		}

		PagingData<Product> pagingData = new PagingData<>();
		pagingData.setData(productList);
		pagingData.setRecordsTotal(count);
		return new ProductExcution<PagingData<Product>>(pagingData, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "规格型号", nickname = "dSpecificationList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "dSpecificationList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DProduceSpecification>> mSpecificationList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DProduceSpecification> list = dSpecificationService.queryProduceDSpecificationList(did);
		return new ProductExcution<List<DProduceSpecification>>(list, ProductEnum.SUCCESS);
	}


	@ApiOperation(value = "单位", nickname = "dUnitList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "dUnitList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DProduceUnit>> dUnitList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DProduceUnit> list = dUnitService.queryProduceDunit(did);
		return new ProductExcution<List<DProduceUnit>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "查询自营商品", nickname = "dProduct", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "dProduct",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DProduceList>> dProduct(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DProduceList> list = mProductInfoService.queryProduct(did);
		return new ProductExcution<List<DProduceList>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "获取经销商商品", nickname = "dAllProduct", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParam(name="mid", value="厂商id",paramType="query",dataType="string")
	@GetMapping(value = "dAllProduct",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DAllProduct>> dAllProduct(HttpServletRequest request,@Validated String mid) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DAllProduct> list = mProductInfoService.queryAllProduce(mid,did);
		return new ProductExcution<List<DAllProduct>>(list, ProductEnum.SUCCESS);
	}

	@ApiOperation(value = "渠道优惠", nickname = "channelDiscount", notes = "" ,tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="mid", value="厂商ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="mpid", value="商品ID",paramType="query",dataType="string")
	})
	@GetMapping(value = "channelDiscount",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<Discount>> channelDiscount(HttpServletRequest request,@Validated @ApiIgnore DiscountDto dto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<Discount> list = mProductInfoService.queryDiscount(dto);
		return new ProductExcution<List<Discount>>(list, ProductEnum.SUCCESS);
	}

}
